package net.stogi.dsper.ui.client.module.menu;

import net.stogi.common.mvp.core.behavior.Behavior;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayoutData;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout.VBoxLayoutAlign;

class MenuWidget extends Composite implements MenuView {

	private static final VBoxLayoutData DEFAULT_BUTTON_LAYOUT_DATA = new VBoxLayoutData(new Margins(0, 0, 5, 0));

	private final ContentPanel mainPanel = new ContentPanel();

	private final Button openDataSourceListButton = new Button();

	private final Button openTagEditorButton = new Button();

	private final Button openUserProfileListButton = new Button();

	public MenuWidget() {
		initComposite();
		initLayout();
		initComponent(mainPanel);
	}

	@Override
	public void appear() {
		mainPanel.show();
		// mainPanel.el().fadeIn(FxConfig.NONE);
	}

	@Override
	public void block() {
		mainPanel.setEnabled(false);
	}

	@Override
	public void disappear() {
		// mainPanel.el().fadeOut(FxConfig.NONE);
		mainPanel.hide();
	}

	@Override
	public void setOpenDataSourceListBehavior(final Behavior behavior) {
		openDataSourceListButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setOpenTagEditorBehavior(final Behavior behavior) {
		openTagEditorButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setOpenUserProfileBehavior(final Behavior behavior) {
		openUserProfileListButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void unblock() {
		mainPanel.setEnabled(true);
	}

	private void initComposite() {
		openDataSourceListButton.setText("Data Sources");
		openTagEditorButton.setText("Tags");
		openUserProfileListButton.setText("User Profiles");

		VBoxLayout layout = new VBoxLayout();
		layout.setVBoxLayoutAlign(VBoxLayoutAlign.STRETCH);

		mainPanel.setHeading("Menu");
		mainPanel.setHeight(115);
		mainPanel.setFrame(true);
		mainPanel.setLayout(layout);
		mainPanel.setVisible(true);
	}

	private void initLayout() {
		mainPanel.add(openUserProfileListButton, DEFAULT_BUTTON_LAYOUT_DATA);
		mainPanel.add(openDataSourceListButton, DEFAULT_BUTTON_LAYOUT_DATA);
		mainPanel.add(openTagEditorButton, DEFAULT_BUTTON_LAYOUT_DATA);
	}
}
